<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends CommonController
{
    public function index(Request $request)
    {
        $data['append']['status'] = $request->status;
        $data['append']['level'] = $request->level;
        $data['append']['search'] = $request->search;

        $data['users'] = \App\User::select('users.*', 'levels.name as level_name', 'images.path as level_logo')
            ->orderBy('level', 'desc')
            ->leftjoin('levels', 'levels.id', '=', 'users.level')
            ->leftjoin('images', 'images.id', '=', 'levels.logo')
            ->where(function ($query) use ($request) {
                if ($request->has('status')) {
                    $query->where('users.status', $request->status);
                }

                if ($request->has('level')) {
                    $query->where('users.level', $request->level);
                }

                if ($request->has('search')) {
                    $query->where('users.name', 'like', '%' . $request->search . '%');
                }
            })
            ->paginate(100);

        $data['levels'] = \App\Level::select('levels.*', 'images.path as logo')
            ->leftjoin('images', 'images.id', '=', 'levels.logo')
            ->get()
            ->toArray();

        return view('admin.user.index', $data);
    }

    //用户编辑
    public function edit(Request $request)
    {
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'name' => 'required',
                'email' => 'required|email',
                'phone' => 'required|phone',
                'describe' => 'required',
                'level' => 'required',
            ]);

            $update['name'] = $request->name;
            $update['email'] = $request->email;
            $update['phone'] = $request->phone;
            $update['level'] = $request->level;
            $update['describe'] = $request->describe;
            $update['status'] = $request->has('status') ? 1 : 0;

            $result = \App\User::where('id', $request->id)->update($update);
            if ($result) {
                $message['message'] = message(true, '编辑成功', '用户 ' . $request->name . ' 信息编辑成功');
            } else {
                $message['message'] = message(false, '编辑失败', '用户 ' . $request->name . ' 信息编辑失败');
            }

            return back()->with($message);
        }

        $data['user'] = \App\User::findorfail($request->id);

        $data['levels'] = \App\Level::where('status', 1)
            ->lists('name', 'id')
            ->toArray();

        return view('admin.user.edit', $data);
    }

    //删除用户
    public function delete(Request $request)
    {
        $user = \App\User::findorfail($request->id);

        $result = $user->delete();

        if ($result) {
            $message['message'] = message(true, '删除成功', '用户 ' . $user->name . ' 删除成功');
        } else {
            $message['message'] = message(false, '删除失败', '用户 ' . $user->name . ' 删除失败');
        }

        return back()->with($message);
    }
}
